package com.syp.hehe.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import com.syp.hehe.dao.ThumbMapper;
import com.syp.hehe.domain.RecordStatusEnum;
import com.syp.hehe.domain.pojo.ThumbSchema;
import com.syp.hehe.domain.vo.ThumbVo;
import com.syp.hehe.service.ThumbServiceI;
import com.syp.hehe.util.paging.Page;
import com.syp.hehe.util.paging.PageOffSet;

@Service("thumbServiceI")
public class ThumbServiceImpl implements ThumbServiceI {
	
	private static final Logger logger = LoggerFactory.getLogger(ThumbServiceImpl.class);
	
	@Autowired
	private ThumbMapper thumbMapper;

	@Override
	public void up(ThumbVo thumb) {
		ThumbSchema thumbSchema = new ThumbSchema();

		thumbSchema.setUuid(thumb.getUuid());
		thumbSchema.setArticleId(thumb.getArticleId());
		thumbSchema.setUsername(thumb.getUsername());
		thumbSchema.setStatus(RecordStatusEnum.NORMAL.getStatus());
		thumbSchema.setCreateTime(Calendar.getInstance().getTime());

		try{
			this.thumbMapper.insert(thumbSchema);
		} catch (DuplicateKeyException e) {
			logger.error(String.format("主键冲突，您已经{%s}收藏了该文章{%d}。", thumb.getUsername(), thumb.getArticleId()));
		}
	}

	@Override
	public void unup(String username, Long articleId) {
		this.thumbMapper.deleteByUnameAndArtid(username, articleId);		
	}

	@Override
	public Long countOfups(Long articleId) {		
		return this.thumbMapper.countByArtid(articleId);
	}

	@Override
	public Boolean upByUser(String username, Long articleId) {
		Long count = this.thumbMapper.countByUnameAndArtid(username, articleId);
		return count > 0L;
	}

	@Override
	public ThumbVo getByUUID(String uuid) {
		ThumbSchema thumbSchema = this.thumbMapper.getByUUID(uuid);
		
		ThumbVo thumb = new ThumbVo();
		thumb.setId(thumbSchema.getId());
		thumb.setUuid(thumbSchema.getUuid());
		thumb.setArticleId(thumbSchema.getArticleId());
		thumb.setUsername(thumbSchema.getUsername());
		thumb.setCreateTime(thumbSchema.getCreateTime());
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		thumb.setCreateTimeText(df.format(thumbSchema.getCreateTime()));
		
		return thumb;
	}

	@Override
	public ThumbVo getByUnameAndArtid(String username, Long articleId) {
		ThumbSchema thumbSchema = this.thumbMapper.getByUnameAndArtid(username, articleId);
		
		ThumbVo thumb = new ThumbVo();
		thumb.setId(thumbSchema.getId());
		thumb.setUuid(thumbSchema.getUuid());
		thumb.setArticleId(thumbSchema.getArticleId());
		thumb.setUsername(thumbSchema.getUsername());
		thumb.setCreateTime(thumbSchema.getCreateTime());
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		thumb.setCreateTimeText(df.format(thumbSchema.getCreateTime()));
		
		return thumb;
	}

	@Override
	public Page<ThumbVo> getPageList(Long articleId, Long pageNum, Integer pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageOffSet<ThumbVo> getPageOffSetList(Long articleId, Long referId, Integer pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

}
